package projects

import (
	"errors"
	"github.com/beego/beego/v2/client/orm"
	"time"
	"xpms/app/models"
	"xpms/cores/utils"
)

type NeedsSwimlanes struct {
	Id          int64 `orm:"pk;column(id);" json:"Id,string"`
	Projectid   int64 `json:"Projectid,string"`
	Name        string
	Type        int
	Userid      int64 `json:"Userid,string"`
	Created     int64
	Changed     int64
	Status      int
	Xcoordinate float64
	Ycoordinate float64
	Colour      string
	Orders      int
	High        int
	Width       int
	Planid      int64 `json:"Planid,string"`
}

func (this *NeedsSwimlanes) TableName() string {
	return models.TableName("projects_needs_swimlanes")
}
func init() {
	orm.RegisterModel(new(NeedsSwimlanes))
}

func AddNeedsSwimlanes(upd NeedsSwimlanes) error {
	o := orm.NewOrm()
	needs := new(NeedsSwimlanes)
	needs.Id = upd.Id
	needs.Projectid = upd.Projectid
	needs.Userid = upd.Userid
	needs.Name = upd.Name
	needs.Created = time.Now().Unix()
	needs.Status = upd.Status
	needs.Xcoordinate = upd.Xcoordinate
	needs.Ycoordinate = upd.Ycoordinate
	needs.Colour = upd.Colour
	needs.Orders = upd.Orders
	needs.High = upd.High
	needs.Width = upd.Width
	needs.Changed = time.Now().Unix()
	_, err := o.Insert(needs)
	return err
}

func UpdateNeedsSwimlanes(id int64, upd NeedsSwimlanes) error {
	var needs NeedsSwimlanes
	o := orm.NewOrm()
	needs.Changed = time.Now().Unix()
	var col []string
	col = append(col, "changed")
	if !utils.IsEmpty(upd.Name) {
		needs.Name = upd.Name
		col = append(col, "name")
	}
	if !utils.IsEmpty(upd.Status) {
		needs.Status = upd.Status
		col = append(col, "status")
	}
	if !utils.IsEmpty(upd.Xcoordinate) {
		needs.Xcoordinate = upd.Xcoordinate
		col = append(col, "xcoordinate")
	}
	if !utils.IsEmpty(upd.Ycoordinate) {
		needs.Ycoordinate = upd.Ycoordinate
		col = append(col, "ycoordinate")
	}
	if !utils.IsEmpty(upd.Colour) {
		needs.Colour = upd.Colour
		col = append(col, "colour")
	}
	if !utils.IsEmpty(upd.Orders) {
		needs.Orders = upd.Orders
		col = append(col, "orders")
	}
	if !utils.IsEmpty(upd.High) {
		needs.High = upd.High
		col = append(col, "high")
	}
	if !utils.IsEmpty(upd.Width) {
		needs.Width = upd.Width
		col = append(col, "width")
	}
	_, err := o.Update(&needs, col...)
	return err
}

func GetNeedsSwimlanesByProjectId(projectid int64) []NeedsSwimlanes {
	var needs []NeedsSwimlanes
	o := orm.NewOrm()
	_, err := o.QueryTable(models.TableName("projects_needs_swimlanes")).Filter("Projectid", projectid).All(&needs)
	if err == orm.ErrNoRows {
		return needs
	}
	return needs
}

func IfsDeleteSwimlane(ids int64) error {
	o := orm.NewOrm()
	var ExistsCount int
	sql := `select count(1) from  pms_projects_needs where crosswise=? `
	err := o.Raw(sql, ids).QueryRow(&ExistsCount)
	if ExistsCount > 0 {
		return errors.New("该泳道下存在卡片")
	}
	return err
}

func DeleteSwimlane(ids int64) error {
	o := orm.NewOrm()
	_, err := o.Raw("DELETE FROM "+models.TableName("projects_needs_swimlanes")+" WHERE id =?", ids).Exec()
	return err
}

func SwimlaneChangePlan(userid int64, id int64, dto SwimlaneToPlanDto) error {
	o := orm.NewOrm()
	swimlanes := NeedsSwimlanes{Id: id}
	err := o.Read(&swimlanes)
	if err == orm.ErrNoRows {
		return errors.New("查询不到")
	} else if err == orm.ErrMissPK {
		return errors.New("找不到主键")
	}
	//创建计划
	planid := utils.SnowFlakeId()
	pro := new(ProjectsPlan)
	pro.Id = planid
	pro.Projectid = swimlanes.Projectid
	pro.Createid = userid
	pro.Swimlanesid = id
	pro.Title = swimlanes.Name
	pro.Vesionno = swimlanes.Name
	pro.Started = dto.Started
	pro.Ended = dto.Ended
	pro.Created = time.Now().Unix()
	pro.Changed = time.Now().Unix()
	swimlanes.Planid = planid
	var needs []ProjectsNeeds
	o.Raw(`SELECT  *  FROM pms_projects_needs WHERE crosswise=?`, id).QueryRows(&needs)
	var tasks []ProjectsTask
	utils.UUID.Init(1024)
	for i, v := range needs {
		task := new(ProjectsTask)
		id, _ := utils.UUID.GetId()
		task.Id = id
		task.Classify = 2
		task.Needsid = v.Id
		task.Name = v.Name
		task.Desc = v.Desc
		task.Projectid = v.Projectid
		task.Planid = planid
		task.Acceptance = v.Acceptance
		task.Orders = i
		task.Userid = userid
		task.Started = time.Now().Unix()
		task.Created = time.Now().Unix()
		task.Changed = time.Now().Unix()
		tasks = append(tasks, *task)
	}
	to, err := o.Begin()
	_, err = to.Insert(pro)
	if err != nil {
		to.Rollback()
		return err
	}
	_, err = o.InsertMulti(100, &tasks)
	if err != nil {
		to.Rollback()
		return err
	}
	_, err = o.Update(&swimlanes, "planid")
	if err != nil {
		to.Rollback()
		return err
	}
	to.Commit()
	return err
}
